package com.jingtao.product.controller;

import com.github.pagehelper.PageInfo;
import com.jingtao.common.core.domain.R;
import com.jingtao.product.domain.DTO.ProductAttrInfo;
import com.jingtao.product.domain.DTO.ProductAttributeParam;
import com.jingtao.product.domain.ProductAttribute;
import com.jingtao.product.domain.ProductAttributeCategory;
import com.jingtao.product.domain.Result.CommonPage;
import com.jingtao.product.domain.Result.CommonResult;
import com.jingtao.product.domain.request.ProductAttrRequest;
import com.jingtao.product.service.ProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @ClassName:
 * @Description:
 * @Author: zengfanzhen
 * @Date: 2024/4/1 20:07:14
 */
@RestController
@Api(tags = "商品属性管理") // 控制器类注解，标记为商品属性管理
@RequestMapping("/productAttribute")
@Slf4j // 日志注解，使用Slf4j日志打印
public class ProductAttributeController {

    @Autowired
    private ProductAttributeService productAttributeService;

//    @ApiOperation("根据分类查询属性列表或参数列表")
//    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
//    @RequestMapping(value = "/list/{cid}", method = RequestMethod.GET)
//    @ResponseBody
//    public CommonResult<CommonPage<ProductAttribute>> getList(@PathVariable Long cid,
//                                                                 @RequestParam(value = "type") Integer type,
//                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
//                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        List<ProductAttribute> productAttributeList = ProductAttrService.getList(cid, type, pageSize, pageNum);
//        return CommonResult.success(CommonPage.restPage(productAttributeList));
//    }

    @ApiOperation("添加商品属性信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ProductAttributeParam productAttributeParam) {
        int count = productAttributeService.create(productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品属性信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody ProductAttributeParam productAttributeParam) {
        int count = productAttributeService.update(id, productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询单个商品属性")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProductAttribute> getItem(@PathVariable Long id) {
        ProductAttribute productAttribute = productAttributeService.getItem(id);
        return CommonResult.success(productAttribute);
    }

    @ApiOperation("批量删除商品属性")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = productAttributeService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据商品分类的id获取商品属性及属性分类")
    @RequestMapping(value = "/attrInfo/{productCategoryId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ProductAttrInfo>> getAttrInfo(@PathVariable Long productCategoryId) {
        List<ProductAttrInfo> productAttrInfoList = productAttributeService.getProductAttrInfo(productCategoryId);
        return CommonResult.success(productAttrInfoList);
    }

    /**
     * 获取所有商品属性分类
     *
     * @param productAttrRequest 请求体，商品属性请求对象
     * @return 响应对象，包含所有商品属性分类信息
     */
    @ApiOperation("获取所有商品属性分类") // API操作注解，描述获取所有商品属性分类的操作
    @PostMapping("/getAllProductAttrCategory")
    public R getAllProductAttrCategory(@RequestBody ProductAttrRequest productAttrRequest) {
        // 打印日志，查询商品信息
        log.info("查询商品信息：{}", productAttrRequest);
        // 获取所有商品属性分类
        List<ProductAttributeCategory> attributeCategoryList = productAttributeService.getAllProductAttrCategory(productAttrRequest);
        // 封装分页信息
        PageInfo<ProductAttributeCategory> pageInfo = new PageInfo<>(attributeCategoryList);
        // 返回成功响应
        return R.ok(pageInfo, "查询成功");
    }


    /**
     *
     * @param cid
     * @param type
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation("根据分类查询属性列表或参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
    @RequestMapping(value = "/list/{cid}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProductAttribute>> getAttrList(@PathVariable Long cid,
                                                                 @RequestParam(value = "type") Integer type,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProductAttribute> productAttributeList = productAttributeService.getList(cid, type, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(productAttributeList));
    }


    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ProductAttributeCategory>> getList(@RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
        List<ProductAttributeCategory> productAttributeCategoryList = productAttributeService.getCateList(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(productAttributeCategoryList));
    }


}



